<?php namespace App\Controllers\Manager;
 
    use Auth, BaseController, Form, Input, Redirect, Sentry, View;
 
    class AuthController extends BaseController {
 
        public function getLogin()
        {
            return View::make('manager.auth.login');
        }

        public function getCreate()
        {
            if(Sentry::check())
            {
                return View::make('manager.auth.create');
            }
            else
            {
                return Redirect::route('manager.login');
            }
        }
 
        public function postLogin()
        {
            $credentials = array(
                'email'    => Input::get('email'),
                'password' => Input::get('password')
            );
 
            try
            {
                $user = Sentry::authenticate($credentials, Input::has('rememberme'));
 
                if ($user)
                {
                   return Redirect::route('manager.dashboard.index');
                }
            }
            catch(\Exception $e)
            {
                return Redirect::route('manager.login')->withErrors(array('login' => $e->getMessage()));
            }
        }

        public function postCreate()
        {
            $credentials = array(
                'email'    => Input::get('email'),
                'password' => Input::get('password'),
                'activated' => true
            );

            try
            {
                // Create the user
                $user = Sentry::createUser($credentials);

                // Find the group using the group id
                $adminGroup = Sentry::findGroupById(1);

                // Assign the group to the user
                $user->addGroup($adminGroup);
            }
            catch (\Cartalyst\Sentry\Users\LoginRequiredException $e)
            {
                echo 'Login field is required.';
                exit();
            }
            catch (\Cartalyst\Sentry\Users\PasswordRequiredException $e)
            {
                echo 'Password field is required.';
                exit();
            }
            catch (\Cartalyst\Sentry\Users\UserExistsException $e)
            {
                echo 'User with this login already exists.';
                exit();
            }
            catch (\Cartalyst\Sentry\Groups\GroupNotFoundException $e)
            {
                echo 'Group was not found.';
                exit();
            }

            return Redirect::route('manager.dashboard.index');
        }
 
        public function getLogout()
        {
            Sentry::logout();
 
            return Redirect::route('manager.login');
        }
 
    }